/*
 * Copyright (c) 2019 Cossack Labs Limited
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.cossacklabs.themis;

/**
 * Key byte storage.
 */
abstract class KeyBytes implements IKey {

    final byte[] key;

    /**
     * Creates a new key from byte array.
     *
     * @param key byte array
     *
     * @throws NullArgumentException if `key` is null.
     * @throws InvalidArgumentException if `key` is empty.
     */
    public KeyBytes(byte[] key) {
        if (key == null) {
            throw new NullArgumentException("key cannot be null");
        }
        if (key.length == 0) {
            throw new InvalidArgumentException("key cannot be empty");
        }
        // Copy the key so that changes in the original array do not
        // affect this key.
        this.key = key.clone();
    }

    @Override
    public byte[] toByteArray() {
        // Java arrays are always mutable. Return a copy to the caller
        // so that their modifications do not affect the actual key.
        return this.key.clone();
    }
}
